#include <errno.h>

#include <linux/sched.h>

int sys_open(const char *filename, int flag, int mode)
{
    struct file *f;
    int i, fd;

    for(fd = 0; fd < NR_OPEN; fd++)
        if (!current->filp[fd])
            break;
    if (fd >= NR_OPEN)
        return -EINVAL;
    current->close_on_exec &= ~(1 << fd);
    f = 0 + file_table;
    for (i = 0 ; i < NR_FILE; i++, f++)
        if (!f->f_count)
            break;
    if (i >= NR_FILE)
        return -EINVAL;
    (current->filp[fd] = f)->f_count++;
    i = open_namei(filename);
    if (i < 0) {
        current->filp[fd] = NULL;
        f->f_count = 0;
        return i;
    }
    return (fd);
}